Skip to main content

ustring

The xtd::ustring class represents text as a sequence of UTF-8 code units.

A string is a sequential collection of characters that's used to represent text. An xtd::ustring object is a sequential collection of char that represent a string; a char corresponds to a UTF-8 code unit. The value of the xtd::ustring object is the content of the sequential collection of char, and unlike std::string that value is immutable (that is, it is read-only).

xtd::ustring vs std::string

xtd::ustring is an immutable std::string.

Additional methods

  • xtd::ustring::concat concatenates two, three or four specified instances of string or string array.
  • xtd::ustring::format writes the text representation of the specified arguments list, to string using the specified format information. See format for more information.
  • xtd::ustring::join concatenates a specified separator string between each element of a specified object array, yielding a single concatenated string.
  • xtd::ustring::parse converts a string into a value_t type.
  • xtd::ustring::split splits this string into a maximum number of substrings based on the characters in an array.
  • xtd::ustring::try_parse Try to convert a string into a value_t type.
  • End more, see xtd::ustring reference guide for a complete list of methods.

String conversions

xtd::convert_string

The xtd::convert_string class represents API to convert string containers.

The xtd::convert_string class is used to convert string container to another.

Remarks

The content of the string must be encoded in UTF-8 (65001) format. Make sure it is, otherwise the result will be uncertain.

Examples

The following code shows how to convert xtd::ustring to std::u32string and writes the converted string to the console output.

#include <xtd/xtd>

using namespace std;
using namespace xtd;

auto main()->int {
ustring s1 = "UTF-8 (65001) string value";

auto s2 = convert_string::to_u32string(s1);
console::write_line(s2);
}

This example is one of N. You can convert any string container to any other string container.

See type convertion for more information and xtd::convert_string class for a complete list of conversion methods.

as operator

The xtd::as<...> operators cast a type into another type.

Remarks

The content of the string must be encoded in UTF-8 (65001) format. Make sure it is, otherwise the result will be uncertain.

Examples

The following code shows how to convert std::u16string to xtd::ustring and writes the converted string to the console output.

#include <xtd/xtd>

using namespace std;
using namespace xtd;

auto main()->int {
u16string s1 = u"UTF-8 (65001) string value";

auto s2 = as<ustring>(s1);
console::write_line(s2);
}

This example is one of N. You can convert any string container to any other string container.

See type convertion for more information and xtd::as<...> operators for a complete list of conversion operators.

xtd::ustring literal operators

There are some literals operators for xtd::ustring:

  • _s literal operator is used to convert specified value into xtd::ustring.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = "This is a "_s + "simple string"_s;
console::write_line(s);
}
// output: This is a simple string
  • _sb literal operator is used to convert the specified value to xtd::ustring with a binary format.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 42_sb;
console::write_line("0b{}", s);
}
// output: 0b101010
  • _sb2 literal operator is used to convert the specified value to xtd::ustring with a binary format with two digits.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 1_sb2;
console::write_line("0b{}", s);
}
// output: 0b01
  • _sb4 literal operator is used to convert the specified value to xtd::ustring with a binary format with four digits.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 3_sb4;
console::write_line("0b{}", s);
}
// output: 0b0011
  • _sb8 literal operator is used to convert the specified value to xtd::ustring with a binary format with eight digits.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 42_sb8;
console::write_line("0b{}", s);
}
// output: 0b00101010
  • _sb16 literal operator is used to convert the specified value to xtd::ustring with a binary format with sixteen digits.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 42_sb16;
console::write_line("0b{}", s);
}
// output: 0b0000000000101010
  • _sb32 literal operator is used to convert the specified value to xtd::ustring with a binary format with thirty two digits.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 42_sb32;
console::write_line("0b{}", s);
}
// output: 0b00000000000000000000000000101010
  • _sb64 literal operator is used to convert the specified value to xtd::ustring with a binary format with sixty four digits.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 42_sb64;
console::write_line("0b{}", s);
}
// output: 0b0000000000000000000000000000000000000000000000000000000000101010
  • _sd literal operator is used to convert specified value into xtd::ustring with a decimal format.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 42_sd;
console::write_line(s);
}
// output: 42
  • _sd literal operator is used to convert specified value into xtd::ustring with a decimal format.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 4.2_sd;
console::write_line(s);
}
// output: 4.2
  • _sx literal operator is used to convert the specified value to xtd::ustring with a hexa format.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 42_sx;
console::write_line("0x{}", s);
}
// output: 0x2a
  • _sx2 literal operator is used to convert the specified value to xtd::ustring with a hexa format with two digits.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 10_sx2;
console::write_line("0x{}", s);
}
// output: 0x0a
  • _sx4 literal operator is used to convert the specified value to xtd::ustring with a hexa format with four digits.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 42_sx4;
console::write_line("0x{}", s);
}
// output: 0x002a
  • _sx8 literal operator is used to convert the specified value to xtd::ustring with a hexa format with eight digits.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 42_sx8;
console::write_line("0x{}", s);
}
// output: 0x0000002a
  • _sX literal operator is used to convert the specified value to xtd::ustring with a hexa format.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 42_sX;
console::write_line("0x{}", s);
}
// output: 0x2A
  • _sX2 literal operator is used to convert the specified value to xtd::ustring with a hexa format with two digits.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 10_sX2;
console::write_line("0x{}", s);
}
// output: 0x0A
  • _sX4 literal operator is used to convert the specified value to xtd::ustring with a hexa format with four digits.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 42_sX4;
console::write_line("0x{}", s);
}
// output: 0x002A
  • _sX8 literal operator is used to convert the specified value to xtd::ustring with a hexa format with eight digits.
#include <xtd/xtd>

using namespace xtd;

auto main()->int {
ustring s = 42_sX8;
console::write_line("0x{}", s);
}
// output: 0x0000002A

See lietarl operators for more information.

See also

​​